import axios from 'axios'
import { Toast } from 'vant'
/**
 * axios实例
 */
const service = axios.create({
    validateStatus(status) {
        return status >= 200 && status < 504
    },
    baseURL: process.env.VUE_APP_BASE_URL,
    timeout: 15000
})

/**
 * 请求重试配置
 */
service.defaults.retry = 3
service.defaults.retryDelay = 1000
service.defaults.shouldRetry = true

/**
 * 请求拦截器配置
 */
service.interceptors.request.use(
    config => {
        /**
         * 请求头信息配置
         */
        config.headers['Content-Type'] = 'application/json'
        config.headers['Accept-Language'] = 'zh-CN'
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

/**
 * 响应拦截器
 */
service.interceptors.response.use(
    response => {
        const res = response.data
        if (res.code === '0000') {
            return Promise.resolve(res)
        } else {
            Toast(res.msg)
            return Promise.reject(res)
        }
    },
    error => {
        return Promise.reject(error)
    }
)

export default service